qui {
local subsample = "$subsample"
****************************************************************************************************************
foreach rhs in $RHSlist { 
	local cnt=0
	forvalues Kpaper=$Kmain(1)$Kmain {
		foreach lhs in $LHSlist { 
			foreach robust in $robust_list {
				local cnt=`cnt'+1
				use "$datapath/004_hom_results_`subsample'_`lhs'_`robust'_K`Kpaper'.dta", clear
				keep b_* d_* dt_*
				merge 1:1 _n using "$datapath/004_hom_results_`subsample'_`lhs'_`robust'_K`Kpaper'BOOT.dta"
				drop _merge
				if `cnt'==1 {
					tempfile temp_`rhs'
					save `temp_`rhs'', replace
				}
				else {
					append using `temp_`rhs''
					save `temp_`rhs'', replace		
				}
			}
		}
	}
}

// Make Tables
local fulllist="NaiveN Naive HetTobit HetSymm HetUni"	
local list="Naive HetTobit HetSymm HetUni"		
local shortlist="HetTobit HetSymm HetUni"		
foreach rhs in $RHSlist { 
	if "`rhs'"=="avg_weeks_worked_3years" {
		local factor=20
	}
	else {
		local factor=100
	}
	forvalues Kpaper=$Kmain(1)$Kmain {
		// beta
		local cnt=0
		foreach lhs in $LHSlist { 
			foreach var in `fulllist' {
				local cnt=`cnt'+1
				use `temp_`rhs'', clear
				keep if LHS=="`lhs'" // must have one observation only after this restriction
				keep b_`var' se_`var'
				gen t_`var'=abs(b_`var'/se_`var')
				replace b_`var' = round(b_`var'*`factor',0.0001)
				replace se_`var' = round(se_`var'*`factor',0.0001)
				rename b_`var' beta_`lhs'
				rename se_`var' betase_`lhs'
				rename t_`var' t_`lhs'	
				order beta_`lhs' betase_`lhs' t_`lhs'
				xpose, clear varname	
				gen id2=_n	
				if v1[3]>=1.96 {
					tostring v1, replace force format(%05.3f)
					replace v1=v1+"**" in 1
					replace v1="("+v1+")   " in 2
				}
				else if v1[3]>=1.645 {
					tostring v1, replace force format(%05.3f)
					replace v1=v1+"*" in 1
					replace v1="("+v1+")   " in 2
				}
				else {
					tostring v1, replace force format(%05.3f)
					replace v1=v1+"" in 1
					replace v1="("+v1+")   " in 2	
				}
				order _varname, first
				rename v1 `var'
				gen LHS="`lhs'"	
				drop if _varname=="t_`lhs'"
				gen coeff="beta"
				if `cnt'==1 {
					tempfile temp1
					save `temp1', replace
				}
				else {
					merge 1:1 _varname using `temp1'
					drop _merge
					save `temp1', replace		
				}
			}
		}
		tempfile tempA
		save `tempA', replace
		// delta
		local cnt=0
		foreach lhs in $LHSlist { 
			foreach var in `shortlist' {
				local cnt=`cnt'+1
				use `temp_`rhs'', clear
				keep if LHS=="`lhs'" // must have one observation only after this restriction
				keep d_`var' se_d_`var'
				gen t_`var'=abs(d_`var'/se_d_`var')
				replace d_`var' = round(d_`var'*`factor',0.0001)
				replace se_d_`var' = round(se_d_`var'*`factor',0.0001)
				rename d_`var' delta_`lhs'
				rename se_d_`var' deltase_`lhs'
				rename t_`var' t_`lhs'	
				order delta_`lhs' deltase_`lhs' t_`lhs'
				xpose, clear varname	
				gen id2=_n	
				if v1[3]>=1.96 {
					tostring v1, replace force format(%05.3f)
					replace v1=v1+"**" in 1
					replace v1="("+v1+")   " in 2
				}
				else if v1[3]>=1.645 {
					tostring v1, replace force format(%05.3f)
					replace v1=v1+"*" in 1
					replace v1="("+v1+")   " in 2
				}
				else {
					tostring v1, replace force format(%05.3f)
					replace v1=v1+"" in 1
					replace v1="("+v1+")   " in 2	
				}
				order _varname, first
				rename v1 `var'
				gen LHS="`lhs'"	
				drop if _varname=="t_`lhs'"
				gen coeff="delta"
				if `cnt'==1 {
					tempfile temp1
					save `temp1', replace
				}
				else {
					merge 1:1 _varname using `temp1'
					drop _merge
					save `temp1', replace		
				}
			}
		}
		tempfile tempB
		save `tempB', replace

		use `tempA', clear
		append using `tempB'

		sort LHS coeff id2
		order LHS coeff id2
		replace _varname=""
		replace _varname="Cognitive" if LHS=="cog" & id2==1 & coeff=="beta"
		replace _varname="Non-Cognitive" if LHS=="noncog" & id2==1 & coeff=="beta"

		replace coeff="\(\delta\)" if coeff=="delta" & id==1 
		replace coeff="Top Bunch" if coeff=="topb" & id==1
		replace coeff="\(\beta\)" if coeff=="beta" & id==1 

		replace coeff="" if id==2
		label variable _varname ""

		label variable NaiveN "Uncorrected No Controls"
		label variable Naive "Uncorrected w/ Controls"
		*label variable Tobit "Tobit"
		label variable HetTobit "Het. \par Tobit"
		label variable HetSymm "Het. Symmetric"
		label variable HetUni "Het. \par  Uniform"
		order _varname coeff NaiveN Naive HetTobit HetUni HetSymm 
		texsave _varname coeff NaiveN Naive HetTobit HetUni HetSymm using "$outpath/005_hom_estimates_`subsample'_`rhs'_K`Kpaper'.tex", replace ///
			varlabels hlines(4) size(5) align(c|CCCCCC) rowsep(.05in) ///
			headerlines("& & (i) & (ii) & (iii) & (iv) & (v)") //frag
	}
}
}
